<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */

	include_once('../sys/sys.php');
	include_once('dm5.php');
	header('Content-Type: text/html; charset=utf-8');
	/*//获取总页数
	$total = get_picurl(2404,0,true) - 3;
	for ($i = 0; $i < $total; $i++) {
		echo get_picurl(2404,$i);
		echo '<br>';
	}*/
	$tid = $_GET['tid'];
	
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	
	$url = 'http://cnc.dm5.com/Type.aspx?id=' . $tid;
	$url_pre = 'http://cnc.dm5.com/';
	
	
	//分离URL以及ID
	$id = $tid;
		
		
	//获取URL内容
//	$url = "./data/1.htm";
	$content = 	file_get_contents($url);
//	$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
	
	//获取名称
	$name = explode('<span class="redzic">',$content);
	$name = $name[1];
	$name = explode('</span></div><div class="sya02">',$name);
	$name = $name[0];
	
	//获取最后更新时间
	$dt = explode('</a><br />更新时间：',$content);
	$dt = $dt[1];
	$dt = explode('<br /><b>',$dt);
	$dt = strtotime(trim($dt[0]));
	if ($dt <= 0) {
		$dt = 0;
	}

	//获取URL列表
	$urls = explode('</div><div class="syd">',$content);
	$urls = $urls[1];
	$urls = explode('</div><div class="ff" style="height:6px;">',$urls);
	$urls = $urls[0];
	
	//$replacer = array('target="_blank"','</a></td></tr><tr><td align="center" >','</a></td><td align="center">','</a></td><td align="center" >');
	//$urls = str_replace($replacer,'',$urls);
	$match_str = "/<a href=\"(.*)\" target=\"_blank\">/isU";
	preg_match_all($match_str,$urls,$urln);
	$urln = $urln[1];
	$current_total_num = count($urln);
	
	
	//进入每页进行处理
	
	//插入分类信息
		//检查当前ID是否存在
		$where = 'AND tid= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'COUNT(*) as max','from'   => 'dm5tags_setting','where'	=> "1 " . $where ,)
		);	
		//当已经存在的时候更新日期
		if ($result['max'] > 0) {
			
		}
		//当不存在时插入
		else{
			do_taginsert($id,$dt,$name,0);
		}
	//获取数据库COUNT
	$where = 'AND tid= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'num','from'   => 'dm5tags_setting','where'	=> "1 " . $where ,)
		);	
	$dbcount = $result['num'];
	
	//echo get_picurl(46875,1);
	//exit;
	
	
	foreach ($urln as $k=>$v) {
		//当COUNT小于已经采集的时候，跳过。
		if ($k < $dbcount) {
			continue;
		}
		$current_url = $url_pre . $v;
		//分离ID
		$ttid = explode('showcomic',$current_url);
		$ttid = $ttid[1];
		
		$pcurl = $url_pre . 'display.aspx?id=' . $ttid;
		//使用当前SESSION伪装访问
		cc($pcurl );
		
		//获取每个集的总页数
		$total = get_picurl($ttid,0,true) - 3;
		//采集每个集的内容并入库
		for ($i = 0; $i < $total; $i++) {
			//图片链接入库
			//get_picurl($ttid,$i);
			do_photoinsert($id,get_picurl($ttid,$i));
		}
	}
	//更新计数
	$where = 'tid= ' . $id .'';
	$DB->do_update('dm5tags_setting',
		array(					  
		   'lastupdate'				=> $dt,
		   'num'					=> count($urln),
		 ),
		 $where
	);
	
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		if ($lu <= 0||$lu == '') {
				$lu = 0;
		}
		$result = $DB->do_insert( 'dm5tags_setting', array( 'tid'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'num'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($tid,$url){
		global $DB;
		$result = $DB->do_insert( 'dm5photos', array( 
								   'tid'				=> $tid,
								   'url'					=> $url,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	echo "采集完毕";
	
	
	exit;
	
	
?>